class Login {
    constructor() {
        this.initInfo()
    }
    initInfo() {
        $('#login-btn').on('click', function() {
            let username = $('#u').val()
            let password = $('#p').val()
            var userList = localStorage.getItem('userList')
            if (userList) {
                // 已经有注册过的用户
                userList = JSON.parse(userList)
                    // 查找相同的用户名和密码
                var isExist = userList.some(userinfo => {
                    return userinfo.username === username && userinfo.password === password
                })
                if (isExist) {
                    // 登录成功用户信息本地存储
                    var userinfo = {
                        username,
                        password
                    }
                    $.cookie('userinfo', username, {
                        expires: 7,
                        path: '/'
                    })
                    alert('登陆成功，即将跳转首页')
                    window.open('/index.html')
                } else {
                    alert('用户名或者密码错误，请重试')
                }
            } else {
                // 没有用户数据
                alert('没有用户请先注册')
            }

        })
        $('#reg').on('click', function() {
            let username = $('#user').val()
            let password = $('#passwd').val()
            let password2 = $('#passwd').val()
            let qq = $('#qq').val()
            let userinfo = {
                username,
                password,
                qq
            }
            var userList = localStorage.getItem('userList')
            if (userList) {
                userList = JSON.parse(userList)
                userList.push(userinfo)
                localStorage.setItem('userList', JSON.stringify(userList))
            } else {
                var arr = [userinfo]
                localStorage.setItem('userList', JSON.stringify(arr))
            }
            alert('注册成功')
            window.href = "/html/login.html"
        })
    }
}
new Login()